﻿using QD_Clients.Code.DbContent;
using QD_Clients.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using QD.Tools;
using System.Threading;
using QD.SQLHelper;
using System.IO;
using QD_Clients.PM;

namespace QD_Clients
{
    public partial class QDClientsPlat : Form
    {
        string _time = "当天";
        public QDClientsPlat()
        {
            InitializeComponent();
            this.gridPending.InitGrid();
            this.gridPendingDetail.InitGrid();
            this.gridNew.InitGrid();
            this.gridNewDetail.InitGrid();
        }

        private void QDClientsPlat_Load(object sender, EventArgs e)
        {
            this.Text = this.Text + " - " + DbConfig.Instance.LogonConfig.ServerName;

            CheckForIllegalCrossThreadCalls = false;

            CreateDesktopShortcut();

            AuthorityMag(QDUserHelper.CurUser.IsManager);

            dateTimePicker1.Value = DateTime.Now.AddMonths(-1);
            dateTimePicker2.Value = DateTime.Now;
            GetData();

            string sql = @"select * from (
select QfName,
(select COUNT(*) from Test_BUG where QfCode=a.QfCode and Status='waitingProcess') as bugNum,
(select COUNT(*) from PM_Reqs where QfCode=a.QfCode and Status='waitingProcess') as reqsNum,
(select COUNT(*) from PM_Reqs where QfCode=a.QfCode and Status='toAudit') as stayNum
from QDDataBase a
) t where t.bugNum>0 or t.reqsNum>0
";
            QDResources db = new QDResources();
            DataTable dt = db.Query(sql);
            gridPending.DataSource = dt;

            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            cellStyle.ForeColor = Color.Red;
            cellStyle.Font = new Font("宋体", 9F, FontStyle.Bold);

            foreach (DataGridViewRow dgr in gridPending.Rows)
            {
                if (dgr.Cells["NoBugNum"].Value.ToString() != "0")
                {
                    dgr.Cells["NoBugNum"].Style = cellStyle;
                }
                if (dgr.Cells["NoReqsNum"].Value.ToString() != "0")
                {
                    dgr.Cells["NoReqsNum"].Style = cellStyle;
                }
                if (dgr.Cells["NostayNum"].Value.ToString() != "0")
                {
                    dgr.Cells["NostayNum"].Style = cellStyle;
                }
            }

            string sqlDetail = @"
select * from (
SELECT t1.QfName,t1.CreatorName,
	(select COUNT(*) from Test_BUG ta where ta.QfCode=t1.QfCode and Status='waitingProcess'and ta.CreatorName = t1.CreatorName) as bugNum,
	(select COUNT(*) from PM_Reqs ta where ta.QfCode=t1.QfCode and Status='waitingProcess' and ta.CreatorName = t1.CreatorName) as reqsNum 
FROM (
select a.QfCode,a.QfName,b.CreatorName from QDDataBase a
left join
Test_BUG b on a.QfCode=b.QfCode
UNION
select a.QfCode,a.QfName,b.CreatorName from QDDataBase a
left join
PM_Reqs b on a.QfCode=b.QfCode) t1
) t where t.bugNum>0 or t.reqsNum>0
";
            DataTable dtDetail = db.Query(sqlDetail);
            gridPendingDetail.DataSource = dtDetail;

            foreach (DataGridViewRow dgr in gridPendingDetail.Rows)
            {
                if (dgr.Cells["listNoBugNum"].Value.ToString() != "0")
                {
                    dgr.Cells["listNoBugNum"].Style = cellStyle;
                }
                if (dgr.Cells["listNoReqsNum"].Value.ToString() != "0")
                {
                    dgr.Cells["listNoReqsNum"].Style = cellStyle;
                }
            }

            this.toolStripStatusLabel1.Text = string.Format("{0}  服务器：{1}", DbConfig.Instance.Uid, DbConfig.Instance.LogonConfig.ServerName);
        }
        private void GetData(DateTime? startDate = null, DateTime? endDate = null)
        {
            string strWhere = "";
            switch (_time)
            {
                case "当天":
                    strWhere = " and DateDiff(dd,CreateDate,getdate())=0";
                    break;
                case "本周":
                    strWhere = " and DateDiff(week,CreateDate,getdate())=0";
                    break;
                case "本月":
                    strWhere = " and DateDiff(mm,CreateDate,getdate())=0";
                    break;
                case "本季度":
                    strWhere = " and DateDiff(qq,CreateDate,getdate())=0";
                    break;
                case "本年":
                    strWhere = " and DateDiff(yy,CreateDate,getdate())=0";
                    break;
                default:

                    break;
            }
            if (startDate != null)
            {
                strWhere = " and CreateDate>='" + startDate + "'";
            }
            if (endDate != null)
            {
                if (startDate != null)
                {
                    strWhere += " and CreateDate<='" + endDate + "'";
                }
                else
                {
                    strWhere = " and CreateDate<='" + endDate + "'";
                }

            }
            string sql = string.Format(@"
select * from (
select QfName,
(select COUNT(*) from Test_BUG where QfCode=t1.QfCode {0}) as bugNum,
(select COUNT(*) from PM_Reqs where QfCode=t1.QfCode {0}) as reqsNum 
from QDDataBase t1
) t where t.bugNum>0 or t.reqsNum>0
", strWhere);
            QDResources db = new QDResources();
            DataTable dt = db.Query(sql);
            gridNew.DataSource = dt;
            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            cellStyle.ForeColor = Color.Red;
            cellStyle.Font = new Font("宋体", 9F, FontStyle.Bold);

            foreach (DataGridViewRow dgr in gridNew.Rows)
            {
                if (dgr.Cells["bugNum"].Value.ToString() != "0")
                {
                    dgr.Cells["bugNum"].Style = cellStyle;
                }
                if (dgr.Cells["reqsNum"].Value.ToString() != "0")
                {
                    dgr.Cells["reqsNum"].Style = cellStyle;
                }
            }

            string sqlDetail = string.Format(@"
select * from (
SELECT t1.QfName,t1.CreatorName,
	(select COUNT(*) from Test_BUG ta where ta.QfCode=t1.QfCode and ta.CreatorName = t1.CreatorName {0}) as bugNum,
	(select COUNT(*) from PM_Reqs ta where ta.QfCode=t1.QfCode and ta.CreatorName = t1.CreatorName {0}) as reqsNum 
FROM (
select a.QfCode,a.QfName,b.CreatorName from QDDataBase a
left join
Test_BUG b on a.QfCode=b.QfCode
UNION
select a.QfCode,a.QfName,b.CreatorName from QDDataBase a
left join
PM_Reqs b on a.QfCode=b.QfCode) t1
) t where t.bugNum>0 or t.reqsNum>0
", strWhere);
            DataTable dtDetail = db.Query(sqlDetail);
            gridNewDetail.DataSource = dtDetail;

            foreach (DataGridViewRow dgr in gridNewDetail.Rows)
            {
                if (dgr.Cells["listBugNum"].Value.ToString() != "0")
                {
                    dgr.Cells["listBugNum"].Style = cellStyle;
                }
                if (dgr.Cells["listReqsNum"].Value.ToString() != "0")
                {
                    dgr.Cells["listReqsNum"].Style = cellStyle;
                }
            }
        }
        /// <summary>
        /// 菜单显示隐藏
        /// </summary>
        /// <param name="b"></param>
        private void AuthorityMag(bool b)
        {
            this.产品ToolStripMenuItem.Visible = b;
            this.toolStripMenuItem1.Visible = b;
            this.图标管理ToolStripMenuItem.Visible = b;
            //this.需求ToolStripMenuItem.Visible = true;
            //this.bUGToolStripMenuItem.Visible = true;
            this.tm_platform.Visible = b;
        }
        private void QDClientsPlat_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void QDClientsPlat_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否退出平台", "确认", MessageBoxButtons.YesNo);
            if (dr == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 创建桌面快捷方式
        /// </summary>
        private static void CreateDesktopShortcut()
        {
            string shortcutName = "QD管理平台";
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//获取桌面文件夹路径            
            string shortcutPath = Path.Combine(desktop, string.Format("{0}.lnk", shortcutName));

            string path = Application.ExecutablePath;
            if (!File.Exists(shortcutPath))
            {
                if (PromptHelper.PromptOkCancel("未创建桌面快捷方式，为方便下次使用，是否立即创建？") == DialogResult.OK)
                {
                    ShortcutCreator.CreateShortcutOnDesktop(shortcutName, path, "QD敏捷开发平台端");
                    PromptHelper.Prompt("创建桌面快捷方式成功！");
                }
            }
        }

        private void 产品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Product form = new Product();
            form.Show();
        }

        private void 图标管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IconMag form = new IconMag();
            form.Show();
        }

        private void 需求ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reqs_QDDevPlat form = new Reqs_QDDevPlat();
            form.Show();
        }

        private void bUGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tester.Test_QDDevPlat form = new Tester.Test_QDDevPlat();
            form.Show();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            dynamic owner = sender;
            if (owner.Checked)
            {
                _time = "当天";
                GetData();
            }

        }
        /// <summary>
        /// 本周
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            dynamic owner = sender;
            if (owner.Checked)
            {
                _time = "本周";
                GetData();
            }
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            dynamic owner = sender;
            if (owner.Checked)
            {
                _time = "本月";
                GetData();
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            dynamic owner = sender;
            if (owner.Checked)
            {
                _time = "本季度";
                GetData();
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            dynamic owner = sender;
            if (owner.Checked)
            {
                _time = "本年";
                GetData();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetData(dateTimePicker1.Value, dateTimePicker2.Value);
        }

        private void 用户设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QDUserMag form = new QDUserMag();
            form.Show();
        }

        private void gridPending_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //BUG数双击
            if (e.ColumnIndex == 1)
            {
                Tester.Test_QDDevPlat form = new Tester.Test_QDDevPlat();
                form.Show();
            }
            //需求数双击
            else if (e.ColumnIndex == 2)
            {
                Reqs_QDDevPlat form = new Reqs_QDDevPlat();
                form.Show();
            }
            //待审核需求数双击
            else if (e.ColumnIndex == 3)
            {
                Reqs_QDDevPlat form = new Reqs_QDDevPlat();
                form.Show();
            }
        }

        private void gridNew_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //BUG数双击
            if (e.ColumnIndex == 1)
            {
                Tester.Test_QDDevPlat form = new Tester.Test_QDDevPlat();
                form.Show();
            }
            //需求数双击
            else if (e.ColumnIndex == 2)
            {
                Reqs_QDDevPlat form = new Reqs_QDDevPlat();
                form.Show();
            }
        }

        private void gridNewDetail_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //BUG数双击
            if (e.ColumnIndex == 2)
            {
                Tester.Test_QDDevPlat form = new Tester.Test_QDDevPlat();
                form.Show();
            }
            //需求数双击
            else if (e.ColumnIndex == 3)
            {
                Reqs_QDDevPlat form = new Reqs_QDDevPlat();
                form.Show();
            }
        }

        private void gridPendingDetail_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //BUG数双击
            if (e.ColumnIndex == 2)
            {
                Tester.Test_QDDevPlat form = new Tester.Test_QDDevPlat();
                form.Show();
            }
            //需求数双击
            else if (e.ColumnIndex == 3)
            {
                Reqs_QDDevPlat form = new Reqs_QDDevPlat();
                form.Show();
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            SysUpgrade upgrade = new SysUpgrade();
            upgrade.Show();
        }

        private void 平台用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SysManager manager = new SysManager();
            manager.Show();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            (new SysInsPkg()).Show();
        }

        private void tsmiSysCfgStruUpgrade_Click(object sender, EventArgs e)
        {
            (new SysCfgStruUpgrade()).Show();
        }

        private void tbtn_zhuxiao_Click(object sender, EventArgs e)
        {
            DialogResult dialog = QD.Tools.PromptHelper.PromptOkCancel("确认注销当前用户？");
            if (dialog == DialogResult.OK)
            {
                QDUserHelper.ClearLoginStatus();
                Login login = new Login();
                login.Show();
                this.Hide();
            }
        }

        private void 表单设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PMtableSettings frm = new PMtableSettings();
            frm.Show();
        }

        private void 平台设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PlatformSetting frm = new PlatformSetting();
            frm.Show();
        }
    }
}
